package com.xsk.dou

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import androidx.databinding.DataBindingUtil
import com.xsk.dou.databinding.AdapterListViewBinding
import com.xsk.dou.model.Subject

class ListViewAdapter(var subjects: MutableList<Subject>, val context: Context) : BaseAdapter() {
    lateinit var adapterListViewBinding: AdapterListViewBinding



    override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {

        if (convertView == null) {
            adapterListViewBinding = DataBindingUtil.inflate(
                LayoutInflater.from(context),
                R.layout.adapter_list_view,
                parent,
                false
            )
        } else {
            adapterListViewBinding = DataBindingUtil.getBinding(convertView)!!
        }
        val subject = subjects.get(position)
        adapterListViewBinding.subject = subject

        return adapterListViewBinding.root
    }

    override fun getItem(position: Int): Any {
        return subjects.get(position)
    }

    override fun getItemId(position: Int): Long {
        return subjects.get(position).hashCode().toLong()
    }

    override fun getCount(): Int {
        return subjects.count()
    }
}